package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;

import java.util.List;

@SuppressWarnings("serial")
@TableBind(tableName="point",pkName = "id")
public class Point extends BasicModel<Point> {
    public static final Point dao = new Point();
    public static final String SELECT_ALL = "`id`,`user_id`,`point_available`,`point_frozen`,`type` ";

    /**
     * 积分类型
     */
    public enum PointType{
        COIN(0,"叮币"),
        BEAN(1,"叮豆"),
        COUPON(2,"叮卷");

        private int type;
        private String name;

        PointType(int type, String name){
            this.name = name;
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public int getType() {
            return type;
        }
    }

    public List<Point> findByUser(long userId){
        return find("select * from point where user_id = ? order by type" ,userId);
    }

    /**
     * 根据用户和类型查找积分
     * @param userId 用户ID
     * @param type 积分类型(叮豆，叮币，叮卷)
     */
    public Point findByUserAndType(long userId, int type){
        return findFirst("select * from point where user_id = ? and type = ?", userId, type);
    }

    public Point addPoint(long point){
        long afterPoint = point + this.getPointAvailable();
        this.setPointAvailable(afterPoint);
        return this;
    }

    public Point setId (Long id){
        this.set("id",id);
        return this;
    }

    public Long getId (){
        return this.get("id");
    }

    public Point setUserId (Long userId){
        this.set("user_id",userId);
        return this;
    }

    public Long getUserId (){
        return this.get("user_id");
    }

    public Point setPointAvailable (Long pointAvailable){
        this.set("point_available",pointAvailable);
        return this;
    }

    public Long getPointAvailable (){
        return this.get("point_available");
    }

    public Point setPointFrozen (Long pointFrozen){
        this.set("point_frozen",pointFrozen);
        return this;
    }

    public Long getPointFrozen (){
        return this.get("point_frozen");
    }

    public Point setType(Integer type){
        this.set("type", type);
        return this;
    }

    public Integer getType(){
        return this.get("type");
    }
}
